package com.yw.smartcityforjava.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.yw.smartcityforjava.R;
import com.yw.smartcityforjava.fragment.HomeFragment;
import com.yw.smartcityforjava.fragment.JzfpFragment;
import com.yw.smartcityforjava.fragment.DataFragment;
import com.yw.smartcityforjava.fragment.PersonFragment;
import com.yw.smartcityforjava.fragment.ServiceFragment;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewpage;
    private BottomNavigationView navigation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewpage = (ViewPager) findViewById(R.id.viewpage);
        navigation = (BottomNavigationView) findViewById(R.id.navigation);

        final List<Fragment> fragmentList =  Arrays.asList(
                new HomeFragment()
                , new ServiceFragment()
                , new JzfpFragment()
                , new DataFragment()
                , new PersonFragment());

        viewpage.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        });

        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.home:
                        viewpage.setCurrentItem(0);
                        break;
                    case R.id.service:
                        viewpage.setCurrentItem(1);
                        break;
                    case R.id.jzfp:
                        viewpage.setCurrentItem(2);
                        break;
                    case R.id.news:
                        viewpage.setCurrentItem(3);
                        break;
                    case R.id.person:
                        viewpage.setCurrentItem(4);
                        break;
                }
                return true;
            }
        });


    }

    public void setNavigationSelectedItemId(int itemId){
        navigation.setSelectedItemId(itemId);
    }
}